﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
            this.Visible = false;
            Form1 form1 = new Form1();
            form1.ShowDialog();
            this.Visible = true;
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            string sql = "select * from Table_1";
            dataGridView1.DataSource = Class1.Query(sql);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                label3.Text = "请输入查询的用户名或密码";
                textBox1.Focus();
            }
            else if (!string.IsNullOrEmpty(textBox1.Text))
            {
                string sql = $"select * from Table_1 where Name like '%{textBox1.Text}%'";
                dataGridView1.DataSource = Class1.Query(sql);
                label3.Text = $"已显有关{textBox1.Text}全部数据";
                timer1.Enabled = true;
                textBox1.Focus();
                textBox1.SelectAll();
            }
            else if (!string.IsNullOrEmpty(textBox2.Text))
            {
                string sql = $"select * from Table_1 where Password like '%{textBox2.Text}%'";
                dataGridView1.DataSource = Class1.Query(sql);
                label3.Text = $"已显有关{textBox2.Text}全部数据";
                timer1.Enabled = true;
                textBox2.Focus();
                textBox2.SelectAll();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label3.Text = null;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label3.Text = null;
        }

        private void button5_Click(object sender, EventArgs e)
        {

            label3.Text = "已显示全部数据";
            timer1.Enabled = true;
            string sql = "select * from Table_1";
            dataGridView1.DataSource = Class1.Query(sql);
            textBox1.Focus();
            textBox1.Text = null;
            textBox2.Text = null;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label3.Text = null;
            timer1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                label3.Text = "请输入要添加的用户名和密码";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text))
            {
                label3.Text = "请输入要添加的用户名";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                label3.Text = "请输入要添加的密码";
                textBox2.Focus();
            }
            else
            {
                bool pd = true;
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (textBox1.Text.Equals(dataGridView1.Rows[i].Cells["Name"].Value.ToString()))
                    {
                        pd = false;
                        break;
                    }
                }
                if (pd)
                {
                    string sql = $"insert into Table_1 (Name,Password) values ('{textBox1.Text}','{textBox2.Text}')";
                    Class1.Query(sql);
                    timer1.Enabled = true;
                    label3.Text = "数据添加成功";
                    string cx = "select * from Table_1";
                    dataGridView1.DataSource = Class1.Query(cx);
                    textBox1.Text = null;
                    textBox2.Text = null;

                }
                else
                {
                    timer1.Enabled = true;
                    label3.Text = "用户名已存在添加失败";
                    textBox1.SelectAll();
                    textBox1.Focus();
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                label3.Text = "请输入要删除的用户名和密码";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text))
            {
                label3.Text = "请输入要删除的用户名";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                label3.Text = "请输入要删除的密码";
                textBox2.Focus();
            }
            else
            {
                bool pd = false;
                int i;
                for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (textBox1.Text.Equals(dataGridView1.Rows[i].Cells["Name"].Value.ToString()))
                    {
                        pd = true;
                        break;
                    }
                }
                if (pd)
                {
                    if (textBox2.Text.Equals(dataGridView1.Rows[i].Cells["Password"].Value.ToString()))
                    {
                        string sql = $"delete from Table_1 where Name='{textBox1.Text}'";
                        Class1.Query(sql);
                        timer1.Enabled = true;
                        label3.Text = "数据删除成功";
                        string cx = "select * from Table_1";
                        dataGridView1.DataSource = Class1.Query(cx);
                        textBox1.Text = null;
                        textBox2.Text = null;
                    }
                    else
                    {
                        timer1.Enabled = true;
                        label3.Text = "密码错误删除失败";
                        textBox2.Focus();
                        textBox2.SelectAll();

                    }
                }
                else
                {
                    timer1.Enabled = true;
                    label3.Text = "用户名不存在删除失败";
                    textBox1.Focus();
                    textBox1.SelectAll();
                }
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                label3.Text = "请输入要修改密码的用户名";
                textBox1.Focus();
            }
            else
            {
                bool pd = false;
                int i;
                for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (textBox1.Text.Equals(dataGridView1.Rows[i].Cells["Name"].Value.ToString()))
                    {
                        pd = true;
                        break;
                    }
                }
                if (pd)
                {
                    string xg = Interaction.InputBox("请输入修改内容", "修改","",8);//调用VB的输入框
                    if (textBox1.Text.Equals(xg))
                    {
                        label3.Text = $"修改的内容一致";
                    }
                    else if (xg != "")
                    {

                        string sql = $"update Table_1 set Password='{xg}' where Name='{textBox1.Text}'";
                        dataGridView1.DataSource = Class1.Query(sql);
                        label3.Text = $"密码修改成功";
                        timer1.Enabled = true;
                        string cx = "select * from Table_1";
                        dataGridView1.DataSource = Class1.Query(cx);
                        textBox1.Focus();
                        textBox1.SelectAll();
                    }
                    else
                    {
                        label3.Text = $"未输入任何修改内容";
                    }
                }
                else
                {
                    label3.Text = $"输入的数据不存在无法修改";
                }
            }
            
        }
    }
}
